//
//  NetWorkRequestSerializer.m
//  IPDirectConnection
//
//  Created by lianglibao on 2019/4/22.
//  Copyright © 2019 lianglibao. All rights reserved.
//

#import "NetWorkRequestSerializer.h"
#import "FetchIPAdressManager.h"

@implementation NetWorkRequestSerializer
- (NSMutableURLRequest *)requestWithMethod:(NSString *)method URLString:(NSString *)URLString parameters:(id)parameters error:(NSError *__autoreleasing  _Nullable *)error {
    NSMutableURLRequest *request = [super requestWithMethod:method
                                                  URLString:URLString parameters:parameters error:error];
    // 解析DNS 为 IP 地址
    NSString *ip = [FetchIPAdressManager fetchIPAdressWithURL:request.URL];
    if (ip) {
        [request setValue:request.URL.host forHTTPHeaderField:@"host"];
        request.URL = [NSURL URLWithString:ip];
    }
    return request;
}

- (NSMutableURLRequest *)requestWithMultipartFormRequest:(NSURLRequest *)request writingStreamContentsToFile:(NSURL *)fileURL completionHandler:(void (^)(NSError * _Nullable))handler {
    NSMutableURLRequest *tmpRequest = [super requestWithMultipartFormRequest:request writingStreamContentsToFile:fileURL completionHandler:handler];
    // 解析DNS 为 IP 地址
    NSString *ip = [FetchIPAdressManager fetchIPAdressWithURL:request.URL];
    if (ip) {
        [tmpRequest setValue:request.URL.host forHTTPHeaderField:@"host"];
        tmpRequest.URL = [NSURL URLWithString:ip];
    }
    return tmpRequest;
}

@end
